package dk.mirasola.systemtraining.bridgewidgets.client.ui.editor;

import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.LeafValueEditor;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.RadioButton;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import dk.mirasola.systemtraining.bridgewidgets.client.ui.RenderUtil;
import dk.mirasola.systemtraining.bridgewidgets.shared.model.Seat;

public class SeatEditor extends Composite implements LeafValueEditor<Seat> {
    interface SeatEditorUIBinder extends UiBinder<Widget, SeatEditor> {
    }

    private static final SeatEditorUIBinder uiBinder = GWT.create(SeatEditorUIBinder.class);

    @UiField
    RadioButton north;
    @UiField
    RadioButton east;
    @UiField
    RadioButton south;
    @UiField
    RadioButton west;
    @UiField(provided = true)
    VerticalPanel panel;

    public SeatEditor() {
        panel = new VerticalPanel();
        panel.setVerticalAlignment(HasVerticalAlignment.ALIGN_TOP);
        initWidget(uiBinder.createAndBindUi(this));
        north.setHTML(RenderUtil.renderSeatUppercaseFirst(Seat.NORTH));
        east.setHTML(RenderUtil.renderSeatUppercaseFirst(Seat.EAST));
        south.setHTML(RenderUtil.renderSeatUppercaseFirst(Seat.SOUTH));
        west.setHTML(RenderUtil.renderSeatUppercaseFirst(Seat.WEST));
    }

    public void setValue(Seat seat) {
        if (seat == null) {
            throw new NullPointerException();
        }
        north.setValue(seat.equals(Seat.NORTH));
        east.setValue(seat.equals(Seat.EAST));
        south.setValue(seat.equals(Seat.SOUTH));
        west.setValue(seat.equals(Seat.WEST));
    }


    public Seat getValue() {
        if (north.getValue()) {
            return Seat.NORTH;
        } else if (east.getValue()) {
            return Seat.EAST;
        } else if (south.getValue()) {
            return Seat.SOUTH;
        } else if (west.getValue()) {
            return Seat.WEST;
        }
        throw new IllegalStateException("no radio button selected");
    }
}
